/**
 * @(#)PatientPanel.java
 *
 *
 * @author 
 * @version 1.00 2010/2/20
 */
package drops.gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import drops.control.PatientControl;
import drops.dao.Patient;
import drops.dao.PatientPeer;

public class PatientPanel extends JPanel implements ActionListener, TableModelListener, MouseListener{
	MainFrame mainFrame;
	
	JPanel headerPanel;
	
	Vector patientHeader;
	Vector patientData;

	List <Patient> patientList;
	DefaultTableModel tableModel;
	JTable patientTable;
	JButton addPatientButton, deletePatientButton;
	SearchPatientPanel searchPatientPanel;
	public PatientPanel (MainFrame mainFrame) {
    	this.mainFrame = mainFrame;
		setLayout(new BorderLayout());
		
		//header part
    	headerPanel = new JPanel();
    	//add patient
    	addPatientButton = new JButton("Add Patient");
    	addPatientButton.addActionListener(this);
    	
    	//delete patient
    	deletePatientButton = new JButton("Delete Patient");
    	deletePatientButton.addActionListener(this);
    	
    	//search patient
    	searchPatientPanel = new SearchPatientPanel(mainFrame);

    	headerPanel.add(addPatientButton);
    	headerPanel.add(deletePatientButton);
    	headerPanel.add(searchPatientPanel);
    	
    	
    	patientList = new LinkedList <Patient> ();
    	
   		patientHeader = new Vector();
    	patientData = new Vector();
		Vector rowData = new Vector();   
    	
    	
    	patientHeader.add("#");
        patientHeader.add("First Name");
        patientHeader.add("Middle Name");
        patientHeader.add("Last Name"); 
 		patientHeader.add("Address");
 		patientHeader.add("Contact #"); 
 		patientHeader.add("Age");
 		patientHeader.add("Occupation");
 		patientHeader.add("Status");
 		patientHeader.add("Complain");
    	
      	
      	tableModel = new DefaultTableModel(patientData, patientHeader) {
      		public boolean isCellEditable(int row, int col) {
      			return false;
      		}
      	};
      	
      	tableModel.addTableModelListener(this);
    	patientTable = new JTable(tableModel);
    	patientTable.setAutoCreateRowSorter(true);
    	patientTable.setFillsViewportHeight(false);
    	patientTable.addMouseListener(this);
    	JScrollPane scrollPane = new JScrollPane(patientTable);
		
    	patientTable.setRowHeight(20);
		patientTable.getTableHeader().setReorderingAllowed(false);
		add(headerPanel, BorderLayout.NORTH);
    	add(scrollPane,BorderLayout.CENTER);
    }
    
    private void updateModel() {
    	patientData = new Vector();
    	for (int i=0, size=patientList.size();i<size;i++) {
    		Patient p = patientList.get(i);
			Vector rowData = new Vector();
    		for (int j=0; j<PatientPeer.numColumns;j++) {
    			rowData.add(p.getByPosition(j));
    		}
    		patientData.add(rowData);
    	}
    	tableModel.setDataVector(patientData, patientHeader);
    }
    
    
    
    public void search(String input, String category) {
    	patientList = PatientControl.searchPatient(input, category);
    	updateModel();
    }
    
    public void delete() {
    	int i = patientTable.getSelectedRow();
    	if (i>=0) {
    		int patientNumber=Integer.parseInt((tableModel.getValueAt(i, 0).toString()));
    		PatientControl.deletePatient(patientNumber);
    		tableModel.removeRow(i);
    	}
    	else {
    		JOptionPane.showMessageDialog(this, "No row selected");
    	}
    }

    public void addPatient() {
    	mainFrame.addPatientFrame();
    }
    
    public void search() {
    	searchPatientPanel.search(true);
    }
    
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==addPatientButton) {
			addPatient();
		}
		else if (e.getSource()==deletePatientButton) {
			delete();
		}
	}

	public void refresh() {
    	searchPatientPanel.search(false);
	}
	
	public void tableChanged(TableModelEvent e) {
		if (e.getType()==TableModelEvent.UPDATE) {
		}
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getSource()==patientTable&&arg0.getClickCount()==2) {
			mainFrame.dentalRecordFrame();
			mainFrame.setPatient(Integer.parseInt(tableModel.getValueAt(patientTable.getSelectedRow(), 0).toString()));
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
}